/*!
\copyright  Copyright (c) 2023 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief      AptX Adaptive utilities

*/

#include "aptx_ad_utils.h"

#include <multidevice.h>

#include <logging.h>

#define L2CAP_FILTER_MASK         0xE0 // 1110 0000 - configure for TWM bit and either left or right
#define L2CAP_FILTER_REJECT_LEFT  0xA0 // 1010 0000 - reject left channel
#define L2CAP_FILTER_REJECT_RIGHT 0xC0 // 1100 0000 - reject right channel

#define L2CAP_FILTER_MASK_DISABLED  0x0
#define L2CAP_FILTER_DISABLED       0x0

#define L2CAP_FILTER_ENABLE         TRUE
#define L2CAP_FILTER_DISABLE        FALSE

#define RTP_TWM_BYTE_OFFSET 12

bool AptxAdUtils_SetL2capFilter(Sink sink)
{
    bool result1 = FALSE;
    bool result2 = FALSE;

    uint8 filter_val_local = Multidevice_IsLeft() ? L2CAP_FILTER_REJECT_RIGHT : L2CAP_FILTER_REJECT_LEFT;
    uint8 filter_val_remote = Multidevice_IsLeft() ? L2CAP_FILTER_REJECT_LEFT : L2CAP_FILTER_REJECT_RIGHT;

    result1 = SinkL2capFilterPackets(sink, L2CAP_FILTER_ENABLE, SINK_FILTER_PATH_LOCAL, RTP_TWM_BYTE_OFFSET, L2CAP_FILTER_MASK, filter_val_local);
    result2 = SinkL2capFilterPackets(sink, L2CAP_FILTER_ENABLE, SINK_FILTER_PATH_RELAY, RTP_TWM_BYTE_OFFSET, L2CAP_FILTER_MASK, filter_val_remote);

    DEBUG_LOG_VERBOSE("appKymeraA2dpSetL2capFilter: snk 0x%x, local 0x%x, remote 0x%x, results %d, %d", sink, filter_val_local, filter_val_remote, result1, result2);

    return (result1 && result2);
}


bool AptxAdUtils_DisableL2capFilter(Sink sink)
{
    bool result1 = SinkL2capFilterPackets(sink, L2CAP_FILTER_DISABLE, SINK_FILTER_PATH_LOCAL, 0, L2CAP_FILTER_DISABLED, L2CAP_FILTER_MASK_DISABLED);
    bool result2 = SinkL2capFilterPackets(sink, L2CAP_FILTER_DISABLE, SINK_FILTER_PATH_RELAY, 0, L2CAP_FILTER_DISABLED, L2CAP_FILTER_MASK_DISABLED);

    DEBUG_LOG_VERBOSE("appKymeraA2dpDisableL2capFilter: snk 0x%x, results %d, %d", sink, result1, result2);

    return (result1 && result2);
}


